<?php

namespace IDock\Insurance\TPIC\Request;

use Exception;
use IDock\Insurance\TPIC\Request\AllTPICRequest;
use IDock\Insurance\TPIC\Response\OfferOrderPriceResponse;
use IDock\Insurance\TPIC\Response\SaveVehicleResponse;
use IDock\Insurance\TPIC\Response\VehicleModelResponse;
use IDock\Insurance\TPIC\TPICEnums;
use IDock\Visitor\Premium\PremiumChargingPileVisitor;
use IDock\Visitor\Premium\PremiumCoverageVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\Premium\PremiumUnderwritingVisitor;
use IDock\Visitor\VehicleModelVisitor;

/**
 * 询价
 */
class OfferOrderPriceRequest extends AllTPICRequest
{
    protected $response = OfferOrderPriceResponse::class;

    protected $urlTip = '询价';

    public $route = 'premiumCalculate';

    /**
     * @var PremiumInquiryVisitor $visitor
     */
    protected $visitor;

    public function generate(): array
    {
        $vehicle = $this->visitor->getVehicleVisitor();
        $arr = [
            'order' => [
                'orderNo' => $this->visitor->getExtInfoVisitor()->argc['orderNo'] ?? '',
            ],
            'loop' => [
                'risk' => $this->getRiskList(),
            ],
            'vehicle' => [
//                'negotiatedActualValue' => $vehicle->actualValue ,
//                'driveUseAttribute' => '',//行驶证使用性质
            ],
            'applicant' => $this->getApplicant(),
            'insured' => $this->getInsured(),
            'extend' => ['premiumTrialFlag' => '0']//保费试算标识
        ];
        if ($vehicle->negotiatedValue ) {
            $arr['vehicle']['negotiatedActualValue'] = $vehicle->negotiatedValue;
        }

        if(!empty($this->visitor->biStartData)){
            $arr['loop']['itemKindBS'] = $this->getBiRiskList();
        }
        $coverageList = $this->visitor->getCoverageVisitorList();
        if(!empty($coverageList)) foreach ($coverageList as $value){
            if(in_array($value->coverageCode,['C16002','C16003'])){
                //充电桩
                $arr['newEnergyDevice'] = $this->getChargeList();
            }
        }
        return $arr;
    }


    private function getRiskList(): array
    {
        $list = [];
        if(!empty($this->visitor->biStartData)){
            $list[] = [
                'startDate' => $this->visitor->biStartData,
                'riskCode' => 'BS',
                'effectFlag' => intval($this->visitor->biEffectFlag),
                'insureYears' => 1,
            ];
        }
        if(!empty($this->visitor->ciStartData)){
            $list[] = [
                'startDate' => $this->visitor->ciStartData,
                'riskCode' => 'BZ',
                'effectFlag' => intval($this->visitor->ciEffectFlag),
                'insureYears' => 1,
            ];
        }
        return $list;
    }

    /**
     * @throws Exception
     */
    private function getBiRiskList(): array
    {
        $coverageList = $this->visitor->getCoverageVisitorList();
        $list = [];
        /*** @var PremiumCoverageVisitor $value */
        foreach ($coverageList as $value)
        {
            //剔除交强险代码
            if ($value->coverageCode == 'C01001') {
                continue;
            }

            $arr = [
                'kindCode' => TPICEnums::$coverageCode[$value->coverageCode] ?? '',
                'relatedInd' => '0',
                'insured' => $value->amount,//保额或者比率
            ];

            //车损险保额
            if ($arr['kindCode'] == '0301200' or $arr['kindCode'] == '0401200') {
                $arr['insured'] = $this->visitor->vehicleVisitor->negotiatedValue ?: $this->visitor->vehicleVisitor->actualValue;
            }

            if(in_array($value->coverageCode,['C04001','C05001'])){
                if($value->coverageCode == 'C04001'){
                    $arr['quantity'] = 1;
                }
                if($value->coverageCode == 'C05001'){
                    $arr['quantity'] = $value->quantity;
                }
            }
            if(in_array($value->coverageCode,['C08001','C08002','C08003','C08004','C18001','C18002','C18003','C18004'])){
                $arr['serviceTimes'] = $value->serviceTimes;
            }
            if(in_array($value->coverageCode,['C05004','C02006','C03005','C04004','C15004','C12006','C13005','C14004'])){
                //费率
                $arr['insured'] = round($value->rate / 100,2);
            }
            if(in_array($value->coverageCode,['C05001','C15001'])){
                //乘客险
                $arr['insured'] = $value->unitAmount;
            }
            //新增设备险
            if ($value->coverageCode == 'C02007' or $value->coverageCode == 'C12007') {
                throw new Exception('太平暂不支持新增设备险，请联系系统管理员');
            }

            $list[] = $arr;
        }
        return $list;
    }

    private function getChargeList(): array
    {
        $list = [];
        foreach ($this->visitor->getChargingPileVisitorList() as $val){
            /*** @var  $val PremiumChargingPileVisitor */
            $list[] = [
                'model' => $val->chargingPileType,
                'code' => $val->chargingPileCode,
                'locationAddress' => $val->chargingPileAddress,
                'chargingPileType' => TPICEnums::$chargingPileKind[$val->chargingPileKind] ?? '',
                'installLocation' => TPICEnums::$chargingPileAddressType[$val->chargingPileAddressType] ?? '',
                'chargeLossSumInsured' => $val->lossAmount,
                'chargeLiabilitySumInsured' => $val->liabilityAmount,
            ];
        }
        return $list;
    }

    private function getApplicant(): array
    {
        $arr = [
            'appliName' => $this->visitor->getApplicantVisitor()->name,
            'identifyNumber' => $this->visitor->getApplicantVisitor()->certificateNo,
            'insuredType' => $this->visitor->getApplicantVisitor()->typeCode == 'R1' ? '1' : '2',//关系人类型
            'identifyType' => TPICEnums::$certificateTypeCode[$this->visitor->getApplicantVisitor()->certificateTypeCode] ?? '',//车主证件类型 默认 01 身份证
            'mobilePhone' => $this->visitor->getApplicantVisitor()->mobile,
            'email' => $this->visitor->getApplicantVisitor()->email,
            'appliAddress' => $this->visitor->getApplicantVisitor()->address,
            'identifyStartDate' => $this->visitor->getApplicantVisitor()->certificateStartDate,
            'identifyEndDate' => $this->visitor->getApplicantVisitor()->certificateEndDate,
        ];
        if(empty($arr['identifyType'])){
            throw new Exception("不支持该投保人证件类型");
        }
        if($arr['identifyType'] == '01'){
            //个人类型
        }
        return $arr;
    }

    /**
     * @throws Exception
     */
    private function getInsured(): array
    {
        $arr = [
            'insuredName' => $this->visitor->getInsuredVisitor()->name,
            'identifyNumber' => $this->visitor->getInsuredVisitor()->certificateNo,
            'insuredType' => $this->visitor->getInsuredVisitor()->typeCode == 'R1' ? '1' : '2',//关系人类型
            'identifyType' => TPICEnums::$certificateTypeCode[$this->visitor->getInsuredVisitor()->certificateTypeCode] ?? '',//车主证件类型 默认 01 身份证
            'insuredMobilePhone' => $this->visitor->getInsuredVisitor()->mobile,
            'email' => $this->visitor->getInsuredVisitor()->email,
            'insuredAddress' => $this->visitor->getInsuredVisitor()->address,
            'identifyStartDate' => $this->visitor->getInsuredVisitor()->certificateStartDate,
            'identifyEndDate' => $this->visitor->getInsuredVisitor()->certificateEndDate,
        ];
        if(empty($arr['identifyType'])){
            throw new Exception("不支持该被保人证件类型");
        }
        return $arr;
    }
}
